<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="角色名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="queryData">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleInsert">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.data" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" label="序号" width="100">
			</el-table-column>
			<el-table-column prop="name" label="角色名称" width="180" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="sn" width="300" sortable>
			</el-table-column>
			<el-table-column label="操作" width="250">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="warning" size="small" @click="handleAuth(scope.$index, scope.row)">授权</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑or新增界面-->
		<el-dialog title="编辑/新增" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="角色名称" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="sn" prop="sn">
					<el-input v-model="saveForm.sn" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--授权界面-->
		<el-dialog title="授权" :visible.sync="authFormVisible" :close-on-click-modal="false">
			<el-tree :data="permissionTree"
					 :props="defaultProps"
					 default-expand-all
					 node-key="id"
					 ref="tree"
					 show-checkbox
					 @check-change="handleCheckChange"></el-tree>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="authSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>

	export default {
		data() {
			return {
				authForm:{
					roleId:null,
					permissionList:[]
				},
				rolePermissionIds:[],
				permissionTree:[],
				defaultProps: {
					children: 'children',
					label: 'name'
				},
				//分页显示的数据
				pageInfo:{
					total:0,
					data:[]
				},
				//分页查找的参数
				query:{
					keyword:'',
					pageSize: 5,
					currentPage:1
				},
				listLoading: false,
				sels: [],//列表选中列
				employees:[],//员工（管理员）列表
				saveFormVisible: false,//编辑/新增界面是否显示
				authFormVisible: false,//授权界面是否显示
				saveLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入角色名称', trigger: 'blur' }
					]
				},
				//编辑/新增界面数据
				saveForm: {
					id: null,
					name: '',
					sn:''
				},
			}
		},
		methods: {
			//授权方法(授权按钮)
			authSubmit(){
				this.$http.put("/role/permission",this.authForm)
						.then(res=>{
							res = res.data
							if (res.success){
								//关闭授权界面
								this.authFormVisible = false
								this.$message({
									message: res.msg,
									type: 'success'
								})
							}
						}).catch(() => {

				});
			},
			//tree控件选中
			handleCheckChange(){
				this.authForm.permissionList = this.$refs.tree.getCheckedNodes();
			},
			//授权按钮事件
			handleAuth(index,row){
				//设置role的ID
				this.authForm.roleId = row.id;
				//获取权限tree
				this.selectAll();
				//查找该角色的权限ids
				this.selectRolePermissionById(row.id);
				this.$nextTick(()=>{
					//通过ids回显数据
					this.$refs.tree.setCheckedKeys(this.rolePermissionIds);
				})
				//显示授权界面
				this.authFormVisible = true;
			},
			selectRolePermissionById(id){
				this.$http.put("/role/permission/" + id)
						.then(res=>{
							res = res.data
							this.rolePermissionIds = res.data;
							console.log(this.rolePermissionIds);
						}).catch(() => {

				});
			},
			//查找所有权限（包含子权限）
			selectAll(){
				this.$http.get("/permission")
						.then(res=>{
							res = res.data
							this.permissionTree = res.data;
						}).catch(() => {

				});
			},
			//分页工具栏事件
			handleCurrentChange(val) {
				//当前页
				this.query.currentPage = val;
				this.queryData();
			},
			//分页高级查询搜索方法
			queryData(){
				this.$http.post("/role",this.query)
					.then(res=>{
						res = res.data
						this.pageInfo = res.data;
						this.query.currentPage = 1;

					}).catch(() => {

					});
			},

			//删除事件
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.deleteById(row.id);
				}).catch(() => {

				});
			},
			//通过ID删除数据方法
			deleteById(id){
				this.$http.delete("/role/"+id,{})
						.then(res=>{
							res = res.data;
							this.listLoading = false;
							//重置当前页为第一页
							this.query.currentPage = 1;
							this.queryData();
							if (res.success){
								this.$message({
									message: res.msg,
									type: 'success'
								});
							}
						})
						.catch(res=>{

						})
			},

			//显示编辑界面
			handleEdit: function (index, row) {
				let obj = Object.assign({}, row);
				this.saveForm = obj;
				if (!obj.department){
					this.saveForm.department = {}
				}
				// 父部门回显
				let temp = [];
				if(obj.department.path){
					let array = obj.department.path.split("/");
					//需要当前的ID（当前部门）
					for(let i=1;i<array.length;i++){
						temp.push(parseInt(array[i]));
					}
				}
				this.saveForm.department = temp;

				//显示编辑界面
				this.saveFormVisible = true;
			},
			//显示insert界面
			handleInsert(){
				//清空
				this.saveForm = {
					id: null,
					name: '',
					sn:''
				}
				this.saveFormVisible = true;
			},

			//编辑or新增确认框
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
							//NProgress.start();

							this.saveData();

						});
					}
				});
			},

			//编辑or新增请求方法
			saveData(){
				this.query.keyword = "";
				var form = this.saveForm;
				//将数组转换成对象
				if (form.department){
					this.saveForm.department = {
						id: form.department[form.department.length-1]
					}
				}
				this.$http.put("/role",this.saveForm)
				.then(res=>{
					res = res.data;
					this.saveFormVisible = false;
					this.saveLoading =false;
					this.queryData();
					if (res.success){
						this.$message({
							message: res.msg,
							type: 'success'
						});
					}
				})
				.catch(res=>{

				})
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除事件
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.query.currentPage = 1;
					//NProgress.start();
					this.batchDelete(ids)
				}).catch(() => {

				});
			},
			//通过ID删除数据方法
			batchDelete(ids){
				this.$http.patch("/role",ids)
						.then(res=>{
							res = res.data;
							this.listLoading = false;
							this.queryData();
							if (res.success){
								this.$message({
									message: res.msg,
									type: 'success'
								});
							}
						})
						.catch(res=>{

						})
			},
		},
		mounted() {
			this.queryData();
		}
	}

</script>

<style scoped>

</style>